﻿using Basic.FlowDAL;
using Basic.FlowRemoteModel.FlowAppMsg.Model;
using WeDonekRpc.Helper;

namespace Basic.FlowCollect.lmpl
{
    internal class FlowAppMsgTemplateCollect : IFlowAppMsgTemplateCollect
    {
        private readonly IFlowAppMsgTemplateDAL _BasicDAL;

        public FlowAppMsgTemplateCollect(IFlowAppMsgTemplateDAL basicDAL)
        {
            this._BasicDAL = basicDAL;
        }

        public void Clear(long appId)
        {
            long[] ids = _BasicDAL.Gets(a => a.AppId == appId, a => a.Id);
            if (!ids.IsNull())
            {
                _BasicDAL.Delete(ids);
            }
        }

        public Result[] Gets<Result>(long appId) where Result : class, new()
        {
            return this._BasicDAL.Gets<Result>(a => a.AppId == appId, "Sort");
        }
        public void Sync(long appId, long formId, FlowAppMsgAdd[] adds)
        {
            if (this._BasicDAL.IsExists(c => c.AppId == appId && c.FormId == formId))
            {
                this._BasicDAL.Sets(appId, formId, adds);
            }
            else if (!adds.IsNull())
            {
                this._BasicDAL.Adds(appId, formId, adds);
            }
        }
    }
}
